<template>
  <el-container v-loading.fullscreen.lock="fullscreenLoading" class="home">
    <!-- 头部区域 -->
    <div class="header">
      <div>
        <img src="../assets/heima.png" alt="" />
        <span class="title">电商后台管理系统</span>
      </div>
      <el-button type="primary" @click="logout" class="logoutButton"
        >登出</el-button
      >
    </div>

    <el-container>
      <!-- 展开折叠按钮 -->
      <div>
        <el-radio-group
          v-model="isCollapse"
          style="margin-bottom: 10px"
          class="toggleButton"
          size="small"
        >
          <el-radio-button :label="false">展开</el-radio-button>
          <el-radio-button :label="true">收起</el-radio-button>
        </el-radio-group>
      </div>
      <!-- 侧边栏 -->
      <div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#fff"
          text-color="#818181"
          active-text-color="#0f75ec"
          :unique-opened="true"
          :collapse="isCollapse"
          class="el-menu-vertical-demo aside"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </div>
      <!-- 主体区域 -->
      <div class="main">
        <!-- 路由占位符 -->
        <router-view></router-view>
      </div>
    </el-container>

    <!-- 回到顶部 -->
    <el-backtop>
      <i class="el-icon-top"></i>
    </el-backtop>
  </el-container>
</template>

<script>
import $ from "jquery";
export default {
  data() {
    return {
      activePath: "",
      // toggle切换
      isCollapse: false,
      // 左侧菜单数据
      menuList: "",
      fullscreenLoading: false,
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-3702mima",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
    };
  },
  created() {
    this.fullscreenLoading = true;
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    // 保存链接激活状态
    saveNavState: function (activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
    // 退出登录
    logout: function () {
      // 登出提示弹框
      this.$confirm("是否退出当前用户的登录?", "登出", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 注销token信息
          window.sessionStorage.clear();
          this.$message({
            type: "success",
            message: "登出成功!",
          });
          this.$router.push("/login");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消登出",
          });
        });
    },
    // 获取所有菜单
    getMenuList: async function () {
      const { data: res } = await this.$http.get("/menus");
      this.fullscreenLoading = false;
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      // console.log(res);
    },
  },
};
</script>

<style lang="less" scoped>
@headerHeight: 70px;
@asiderWidth: 200px;
.el-container {
  // overflow: hidden;
  position: relative;
  // height: 100%;
  width: 100%;
}
.header {
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  padding-left: 25px;
  width: 100%;
  height: @headerHeight;
  background-color: #0f75ec;
  color: #fff;
  font-size: 25px;
  font-weight: 600;
  box-shadow: 0 0 10px #555;
  border-radius: 0 0 5px 5px;
  z-index: 2;
  .logoutButton {
    margin-right: 10px;
  }
  > div {
    display: flex;
    align-items: center;
    span {
      display: block;
      padding-left: 15px;
      margin-left: 15px;
    }
  }
}
.aside {
  box-sizing: border-box;
  position: fixed;
  height: 100%;
  padding-top: @headerHeight + 56;
  background-color: #fff;
  box-shadow: 0 0 5px #444;
  z-index: 1;
  .el-menu {
    border-right: none;
  }
}

.toggleButton {
  position: fixed;
  top: @headerHeight + 20;
  left: 5px;
  z-index: 5;
}

.main {
  box-sizing: border-box;
  width: 100%;
  height: auto;
  min-height: 100vh;
  background-color: #f0f5f8;
  padding-top: @headerHeight + 30;
  padding-left: @asiderWidth - 65;
  padding-right: 50px;
  z-index: 0;
}

.iconfont {
  font-size: 18px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu--collapse {
  width: 60px;
}

.title {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}
</style>